#!/bin/sh
#                        2001:db8:3::
# A                   R2--------------R3                     B
#        2001:db8:1::                      2001:db8:5::
#     R1                                                R6
#        2001:db8:2::                      2001:db8:6::
# C                   R4--------------R5                     D
# 

#A
ip netns add A 
ip link add name A_R1 type veth peer name R1_A
ip link set A_R1 netns A 
ip netns exec A bash -c \
'
	ip link set lo up
	ip link set A_R1 up
	ip addr add 192.168.0.1/24 dev A_R1
	ip route add 10.0.0.0/24 via 192.168.0.254 dev A_R1
'

#C
ip netns add C
ip link add name C_R1 type veth peer name R1_C
ip link set C_R1 netns C
ip netns exec C bash -c \
'
	ip link set lo up
	ip link set C_R1 up
	ip addr add 192.168.0.1/24 dev C_R1
	ip route add 10.0.0.0/24 via 192.168.0.254 dev C_R1
'

#R1
ip netns add R1
ip link add name R1_R2 type veth peer name R2_R1
ip link add name R1_R4 type veth peer name R4_R1
ip link set R1_R2 netns R1
ip link set R1_R4 netns R1
ip link set R1_A netns R1
ip link set R1_C netns R1
ip netns exec R1 bash -c \
'
	ip link set lo up
	ip link set R1_C up
	ip link set R1_A up
	ip link set R1_R2 up
	ip link set R1_R4 up
	sysctl net.ipv6.conf.all.forwarding=1
	sysctl net.ipv6.conf.all.seg6_enabled=1
	sysctl net.ipv6.conf.R1_A.seg6_enabled=1
	sysctl net.ipv6.conf.R1_C.seg6_enabled=1
	sysctl net.ipv6.conf.R1_R2.seg6_enabled=1
	sysctl net.ipv6.conf.R1_R4.seg6_enabled=1
	sysctl net.vrf.strict_mode=1

	ip link add name vif_ab type vrf table 100
	ip link add name vif_cd type vrf table 200
	ip link set R1_A master vif_ab
	ip link set R1_R2 master vif_ab
	ip link set R1_C master vif_cd
	ip link set R1_R4 master vif_cd
	ip route add table 100 unreachable default metric 4278198272
	ip link set vif_ab up
	ip link set vif_cd up

	ip addr add 2001:db8:1::12/64 dev R1_R2
	ip addr add 2001:db8:2::14/64 dev R1_R4
	ip addr add fe80::1/64 dev R1_A
	ip addr add fe80::1/64 dev R1_C
	ip addr add fe80::1/64 dev R1_R2
	ip addr add fe80::1/64 dev R1_R4
	ip addr add 192.168.0.254/24 dev R1_A
	ip addr add 192.168.0.254/24 dev R1_C

	ip -6 route add A1::1A encap seg6local action End.DT4 vrftable 100 dev R1_A table 100
	ip -6 route add A2::1 via fe80::2 dev R1_R2 table 100
	ip route add 10.0.0.0/24 encap seg6 mode encap segs A2::1,A3::1,A6::6B dev R1_R2 table 100

	ip -6 route add A1::1C encap seg6local action End.DT4 vrftable 200 dev R1_C table 200
	ip -6 route add A4::1 via fe80::4 dev R1_R4 table 200
	ip route add 10.0.0.0/24 encap seg6 mode encap segs A4::1,A5::1,A6::6D dev R1_R4 table 200

'

#R2
ip netns add R2
ip link add name R2_R3 type veth peer name R3_R2
ip link set R2_R1 netns R2
ip link set R2_R3 netns R2
ip netns exec R2 bash -c \
'
	ip link set lo up
	ip link set R2_R1 up
	ip link set R2_R3 up
	sysctl net.ipv6.conf.all.forwarding=1
	sysctl net.ipv6.conf.all.seg6_enabled=1
	sysctl net.ipv6.conf.R2_R1.seg6_enabled=1
	sysctl net.ipv6.conf.R2_R3.seg6_enabled=1

	ip addr add 2001:db8:3::23/24 dev R2_R3
	ip addr add 2001:db8:1::21/24 dev R2_R1
	ip addr add fe80::2/64 dev R2_R1
	ip addr add fe80::2/64 dev R2_R3

	ip -6 route add A2::1 encap seg6local action End dev R2_R3
	ip -6 route add A3::1 via fe80::3 dev R2_R3
	ip -6 route add A1::1A via fe80::1 dev R2_R1
'

#R3
ip netns add R3
ip link add name R3_R6 type veth peer name R6_R3
ip link set R3_R2 netns R3
ip link set R3_R6 netns R3
ip netns exec R3 bash -c \
'
	ip link set lo up
	ip link set R3_R2 up
	ip link set R3_R6 up
	sysctl net.ipv6.conf.all.forwarding=1
	sysctl net.ipv6.conf.all.seg6_enabled=1
	sysctl net.ipv6.conf.R3_R2.seg6_enabled=1
	sysctl net.ipv6.conf.R3_R6.seg6_enabled=1

	ip addr add 2001:db8:3::32/64 dev R3_R2
	ip addr add 2001:db8:5::36/64 dev R3_R6
	ip addr add fe80::3/64 dev R3_R2
	ip addr add fe80::3/64 dev R3_R6

	ip -6 route add A3::1 encap seg6local action End dev R3_R6
	ip -6 route add A6::6B via fe80::6 dev R3_R6
	ip -6 route add A2::1 via fe80::2 dev R3_R2
'

#R4
ip netns add R4
ip link add name R4_R5 type veth peer name R5_R4
ip link set R4_R1 netns R4
ip link set R4_R5 netns R4
ip netns exec R4 bash -c \
'
	ip link set lo up
	ip link set R4_R1 up
	ip link set R4_R5 up
	sysctl net.ipv6.conf.all.forwarding=1
	sysctl net.ipv6.conf.all.seg6_enabled=1
	sysctl net.ipv6.conf.R4_R1.seg6_enabled=1
	sysctl net.ipv6.conf.R4_R5.seg6_enabled=1

	ip addr add 2001:db8:2::41/64 dev R4_R1
	ip addr add 2001:db8:4::45/64 dev R4_R5
	ip addr add fe80::4/64 dev R4_R1
	ip addr add fe80::4/64 dev R4_R5

	ip -6 route add A4::1 encap seg6local action End dev R4_R5
	ip -6 route add A5::1 via fe80::5 dev R4_R5
	ip -6 route add A1::1C via fe80::1 dev R4_R1
'

#R5
ip netns add R5
ip link add name R5_R6 type veth peer name R6_R5
ip link set R5_R4 netns R5
ip link set R5_R6 netns R5
ip netns exec R5 bash -c \
'
	ip link set lo up
	ip link set R5_R4 up
	ip link set R5_R6 up
	sysctl net.ipv6.conf.all.forwarding=1
	sysctl net.ipv6.conf.all.seg6_enabled=1
	sysctl net.ipv6.conf.R5_R4.seg6_enabled=1
	sysctl net.ipv6.conf.R5_R6.seg6_enabled=1

	ip addr add 2001:db8:4::54/64 dev R5_R4
	ip addr add 2001:db8:6::56/64 dev R5_R6
	ip addr add fe80::5/64 dev R5_R4
	ip addr add fe80::5/64 dev R5_R6

	ip -6 route add A5::1 encap seg6local action End dev R5_R6
	ip -6 route add A4::1 via fe80::4 dev R5_R4
	ip -6 route add A6::6D via fe80::6 dev R5_R6
'


#R6
ip netns add R6
ip link add name R6_B type veth peer name B_R6
ip link add name R6_D type veth peer name D_R6
ip link set R6_R3 	netns R6
ip link set R6_B 	netns R6
ip link set R6_D 	netns R6
ip link set R6_R5 	netns R6
ip netns exec R6 bash -c \
'
	ip link set lo up
	ip link set R6_R3 up
	ip link set R6_R5 up
	ip link set R6_B up
	ip link set R6_D up
	sysctl net.ipv6.conf.all.forwarding=1
	sysctl net.ipv6.conf.all.seg6_enabled=1
	sysctl net.ipv6.conf.R6_R3.seg6_enabled=1
	sysctl net.ipv6.conf.R6_R5.seg6_enabled=1
	sysctl net.ipv6.conf.R6_B.seg6_enabled=1
	sysctl net.ipv6.conf.R6_D.seg6_enabled=1
	sysctl net.vrf.strict_mode=1

	ip link add name vif_ab type vrf table 100
	ip link add name vif_cd type vrf table 200
	ip link set R6_B master vif_ab
	ip link set R6_R3 master vif_ab
	ip link set R6_D master vif_cd
	ip link set R6_R5 master vif_cd
	ip route add table 100 unreachable default metric 4278198272
	ip link set vif_ab up
	ip link set vif_cd up

	ip addr add 2001:db8:5::63/64 dev R6_R3
	ip addr add 2001:db8:6::65/64 dev R6_R5
	ip addr add fe80::6/64 dev R6_R3
	ip addr add fe80::6/64 dev R6_R5
	ip addr add fe80::6/64 dev R6_B
	ip addr add fe80::6/64 dev R6_D
	ip addr add 10.0.0.254/24 dev R6_B
	ip addr add 10.0.0.254/24 dev R6_D

	ip -6 route add A6::6B encap seg6local action End.DT4 vrftable 100 dev R6_B table 100
	ip -6 route add A3::1 via fe80::3 dev R6_R3 table 100
	ip route add 192.168.0.0/24 encap seg6 mode encap segs A3::1,A2::1,A1::1A dev R6_R3 table 100

	ip -6 route add A6::6D encap seg6local action End.DT4 vrftable 200 dev R6_D table 200
	ip -6 route add A5::1 via fe80::5 dev R6_R5 table 200
	ip route add 192.168.0.0/24 encap seg6 mode encap segs A5::1,A4::1,A1::1C dev R6_R5 table 200
'

#B
ip netns add B
ip link set B_R6 netns B
ip netns exec B bash -c \
'
	ip link set lo up
	ip link set B_R6 up

	ip addr add 10.0.0.1/24 dev B_R6
	ip route add 192.168.0.0/24 via 10.0.0.254 dev B_R6
'

#D
ip netns add D
ip link set D_R6 netns D
ip netns exec D bash -c \
'
	ip link set lo up
	ip link set D_R6 up

	ip addr add 10.0.0.1/24 dev D_R6
	ip route add 192.168.0.0/24 via 10.0.0.254 dev D_R6
'

